$(function() {
	var rows_delete=0;
	var mode="add";
	var m_id=0;
    var url='controls/notice_model.php';
	 
if (_dd==1){
	$("#btnAdd").show();
}else{
	$("#btnAdd").hide();
}
	var checkDate = $('.btndate').datepicker().on('changeDate', function(ev) {
	  checkDate.hide();
	}).data('datepicker');
	
	$("#txtDateFrom_s,#txtDateTo_s").val(_cdate);
	
	$.get(url, {"m": "emp"}, function(data) {
        var obj = eval('(' + data + ')');
        if (obj != null) {
            $.each(obj, function(index, text) {
                $("#cboName").append(
                        $('<option></option>').val(text).html(index)
                        );
            });
        }
    });

   // $("#cboRequets").append($('<option></option>').val(0).html('--แสดงทั้งหมด--'));
	
	
	LoadData('',_cdate,_cdate);
	
	
	
	 function LoadData(name,df,dt){
        $( "#resultData tbody tr" ).remove();
 
        
        $('#resultData').dataTable( {
			"bProcessing": true,
			"bFilter": false,
			"bAutoWidth": false,
			"bLengthChange": false,
			"sAjaxSource": url,
			"oLanguage": {
				"sEmptyTable":     "ไม่พบข้อมูล"
			},
			"bDestroy": true,
			"sServerMethod": "POST",
			"fnServerParams": function ( aoData ) {
					aoData.push( { "name": "m", "value": "load" },
								{"name":"name","value":name},
								{"name":"date_from","value":df},
								{"name":"date_to","value":dt}
							);
				},
			"aoColumnDefs":[
				{  "bVisible": false , "aTargets": [ 1 ] },	
			 	{  "bSortable": true , "aTargets": [ 2 ] ,"sClass":"text-center"},	
				{  "bSortable": true , "aTargets": [ 3 ] ,"sClass":"text-left"},	
				{  "bSortable": true , "aTargets": [ 4 ]  ,"sClass":"text-left"},	
				{  "bSortable": true, "aTargets": [ 5 ] ,"sClass":"left"},
				{"aTargets": [6],
                    "sClass": "center",
                    "mData": null,
                    "mRender": function(data, type, full) {
						 
                           return "<a href='#' ><img src='img/print2.png' class='btnPrint' style='cursor:pointer;'/></a>";
                      
                    }
                },
				{"aTargets": [7],
                    "sClass": "center",
                    "mData": null,
                    "mRender": function(data, type, full) {
						if (_te==1)
                           return "<a href='#popupConfirm' data-toggle='modal'><img src='img/delete-file.png' class='btnDel' style='cursor:pointer;'/></a>";
                         else 
							return "";
                    }
                }
				 
			],"fnDrawCallback": function ( oSettings ) {
				 var that = this;
                /* Need to redo the counters if filtered or sorted */
                var iTotalRecords = oSettings.fnRecordsTotal();
                //alert(oSettings.aiDisplay.length + ' ' + iTotalRecords);

                if (iTotalRecords > 0) {
                    if (oSettings.bSorted || oSettings.bFiltered)
                    {
                        for (var i = 0, iLen = iTotalRecords; i < iLen; i++)
                        {
                            $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr).html(i + 1);
                        }
                    }
                }

            }
		 } );
		 
		 $(".btnPrint").live('click',function(){
		 	var form = $(this).closest('form');
			mode="edit";
			var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
			var data =$('#resultData').dataTable().fnGetData(rows);
			
			 m_id = data[1];
			 
			 
			 $("#frmNotice").get(0).setAttribute('action', 'rpt_notice.php');
			 $("#mid").val(m_id);
			 
			 form.attr('target', '_blank').submit().removeAttr('target').removeAttr('action');
			 
		 });
		 
				 //Popup confirm delete
			$(".btnDel").live('click',function(){
				var rows = $(this).closest("tr").get(0);
				rows_delete = $('#resultData').dataTable().fnGetPosition(rows);

			}); 
			
			$("#btnDelete").live('click', function(){
				var row_id =$('#resultData').dataTable().fnGetData(rows_delete);
				
				var id=row_id[1];
				 
				 $.ajax({
						 type: 'POST',
						 cache: false,
						 url: url,
						 data: {"m":"del","id":id},
						 success: function(data){
							$("#popupConfirm").modal('hide');
							$('#resultData').dataTable().fnDeleteRow( rows_delete );
							$('#resultData').dataTable().fnDraw();
						 }
					 });
		 
			}); 
			
      }
	
 
	
	$("#btnSearch").click(function(){
	
		var _name = $("#txtName_s").val();
		var _df = $("#txtDateFrom_s").val();
		var _dt = $("#txtDateTo_s").val();
		
		LoadData(_name,_df,_dt);
	});
	

	$("#btnSave").live('click',function(){
		$("#txtDetail").jqBootstrapValidation({preventSubmit: false,
			submitSuccess: function ($form, event) { event.preventDefault();}
		}); 
		var _detail = $("#txtDetail").val();
		var _noticed = $("#cboName").val();
		var _title = $("#txtTitle").val();
		
		if (_detail !='' && _title!=''){
			
			$.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: {"m": "add", "detail":_detail,"noticed":_noticed,"title":_title},
				success: function(data) {
					//alert(data);
					var _name = $("#txtName_s").val();
					var _df = $("#txtDateFrom_s").val();
					var _dt = $("#txtDateTo_s").val();
					LoadData(_name,_df,_dt);
					 $("#popup").modal('hide');
				}
			});
		}
		
		 $("#txtDetail").jqBootstrapValidation("destroy");
	});		
	 
	$("#btnAdd").live('click', function(){
		mode="add";
		$("#txtDetail").val('');
	});
		
   
	$("#btnPrintDraft").click(function(){
	  $("#resultData").printThis();
		return false;
           
	});  
    
});


